package com.yazi.mxz.mobile.controller;


import java.util.List;

import javax.annotation.Resource;





import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.myframework.smhj.hibernate.Page;
import com.myframework.smhj.hibernate.Searching;
import com.myframework.smhj.hibernate.SearchingFactory;
import com.myframework.smhj.jackson.result.ObjectResult;
import com.myframework.smhj.jackson.result.PageResult;
import com.yazi.mxz.common.entity.beans.StoreGoods;
import com.yazi.mxz.common.entity.enums.StoreGoodsStatus;
import com.yazi.mxz.common.service.StoreGoodsService;
import com.yazi.mxz.mobile.jackson.filter.StoreGoodsFilter;

@Controller("storeGoodsController")
@RequestMapping("/storeGoods")
public class StoreGoodsController extends BaseController {
	private static Logger log = LoggerFactory.getLogger(StoreGoodsController.class);
	
	@Resource
	StoreGoodsService storeGoodsService;
	
	@RequestMapping("/list")
	public String list(){
		log.debug("进入商品列表页面");
		return "v1.5/storeGoods_list";
	}
	
	@RequestMapping("/list4Shop")
	public String list4Shop(){
		log.debug("进入店铺商品列表页面");		
		return "v1.5/storeGoods_list_4_shop";
	}
	
	@RequestMapping("/list/json")
	@ResponseBody
	public PageResult<?> listJson(StoreGoods storeGoods, Searching si){
		log.debug("获取商品列表");
		PageResult<StoreGoods> jsonResult = new PageResult<StoreGoods>();
		jsonResult.setFilter(StoreGoodsFilter.get4List());
		try {
			storeGoods.setStatus(StoreGoodsStatus.ON.ordinal());
			Page<StoreGoods> page = storeGoodsService.list(storeGoods, si);
			jsonResult.setResult(page);
			jsonResult.setOk();
		} catch (Exception e) {
			log.error("获取商品列表异常:", e);
			jsonResult.setErrorMessage("商品列表获取失败!");
		}
		return jsonResult;
	}
	
	@RequestMapping("/detail/{storeGoodsId}")
	public String detail(@PathVariable long storeGoodsId, Model model){
		log.debug("进入商品详情页面");
		StoreGoods storeGoods = storeGoodsService.one(storeGoodsId);
		if (null == storeGoods) {
			return REDIRECT_INDEX;
		}
		
		/* 热门推荐商品 start */
		StoreGoods sg4Recommand = new StoreGoods();
		sg4Recommand.setStatus(StoreGoodsStatus.ON.ordinal());
		sg4Recommand.setCategory(storeGoods.getCategory());// 同类商品
		
		// 按月销量倒序
		Searching si4Recommand = SearchingFactory.createSearching("dealMonth", true, 4);
		
		List<StoreGoods> recommandList = storeGoodsService.list(sg4Recommand, si4Recommand).getList();
		
		/* 热门推荐商品 end */
		
		model.addAttribute("storeGoods", storeGoods);
		model.addAttribute("recommandList", recommandList);
		return "storeGoods_detail";
	}
	
	@RequestMapping("/detail/json")
	@ResponseBody
	public ObjectResult<?> detailJson(long storeGoodsId){
		log.debug("获取商品详情");
		ObjectResult<StoreGoods> jsonResult = new ObjectResult<StoreGoods>();
		jsonResult.setFilter(StoreGoodsFilter.get4Detail());
		StoreGoods storeGoods;
		try {
			storeGoods = storeGoodsService.one(storeGoodsId);
			jsonResult.setResult(storeGoods);
			jsonResult.setOk();
		} catch (Exception e) {
			log.error("获取商品详情异常:", e);
			jsonResult.setErrorMessage("获取商品详情失败!");
		}
		return jsonResult;
	}
	
}
